- open powershell and write this:
start notepad $profile
this script is available when a new powershell session is open. - write the following:
function ImageinClipboard2File($Dest)
{
# Any image available in the clipboard
$img=get-clipboard -format image
if ($img -ne $null) {
# Create the file and the directory if required - No Output
New-Item -Path $Dest -ItemType File -Force | out-null
# Actually save the image into the file
$img.save($Dest)
return "Image found in clipboard and created in $Dest"
}
else {
return "No image found in clipboard"
}
}
then in emacs do add this:
(defvar my/screenshot-directory "./images/")
;; Key function ... Script part commented out // to be improved
(defun my/take-screenshot ()
"This works only in Windows. Take the image in the
clipboard, name it with a timestamp,and store it in png format
into `my/screenshot-directory'. Returns filename if successful, nil if no image found."
(let ((filename)
(result))
;; Use absolute path to avoid permission issues
(setq filename (expand-file-name
(concat (format-time-string "%Y-%m-%dT%H%M%S") ".png")
(expand-file-name my/screenshot-directory)))
;; Ensure directory exists
(unless (file-exists-p (file-name-directory filename))
(make-directory (file-name-directory filename) t))
(setq result (string-trim (shell-command-to-string (concat "Powershell ImageinClipboard2File -Dest '" filename "'"))))
(message "PowerShell result: %s" result)
;; Check for errors or no image - look for error indicators
(if (or (string-match-p "No image found" result)
(string-match-p "Exception" result)
(string-match-p "Access.*denied" result)
(string-match-p "Could not find" result))
(progn
;; Clean up the empty file if it was created
(when (file-exists-p filename)
(delete-file filename))
nil)
;; Also verify the file actually exists and has content
(if (and (file-exists-p filename)
(> (file-attribute-size (file-attributes filename)) 0))
(file-relative-name filename)
nil))))
;; Alternative version with just the basic markdown image syntax
(defun my/markdown-screenshot-simple ()
"Save a screenshot in clipboard into `my/screenshot-directory'.
Insert a simple Markdown image link to the image file."
(interactive)
(let ((filename (my/take-screenshot)))
(message "Filename returned: %s" filename) ; Debug message
(if filename
(progn
(message "Inserting markdown for: %s" filename)
(insert (format "" filename)))
(message "No image found in clipboard - no markdown inserted"))))
;; Alternative keybinding for simple version
(global-set-key (kbd "C-c i s") 'my/markdown-screenshot-simple)
adapted from code found here: https://www.reddit.com/r/emacs/comments/ub4f8v/paste_clipboard_as_image_on_windows/